package com.lantosec.hmrp.modules.system.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.BeanUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.system.entity.SysRole;
import com.lantosec.hmrp.modules.system.mapper.SysRoleMapper;
import com.lantosec.hmrp.modules.system.service.ISysRoleService;
import com.lantosec.hmrp.modules.system.vo.SysMenuVO;
import com.lantosec.hmrp.modules.system.vo.SysPermissionVO;
import com.lantosec.hmrp.modules.system.vo.SysRoleVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-14
 */
@Service
@Primary
@Log4j2
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Override
    public List<SysPermissionVO> queryPermissionByRuleId(String roleId) {
        return baseMapper.queryPermissionByRuleId(roleId);
    }

    @Override
    public List<SysMenuVO> queryMenuByRuleId(String roleId) {
        return baseMapper.queryMenuByRuleId(roleId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveRoleInfo(SysRoleVO vo) {
        /*
         * 新增之前先删除原有的角色信息，权限信息，菜单信息
         * */
        SysRole sysRole = baseMapper.selectById(vo.getRoleId());
        String roleId = vo.getRoleId();
        if (!StringUtils.isEmpty(sysRole)) {
            baseMapper.updateById(BeanUtils.VO2Entity(vo)); //删除角色信息
            baseMapper.deleteRoleMenuInfo(roleId);  //删除角色菜单挂接信息
            baseMapper.deleteRolePermission(roleId);    //删除角色权限挂接信息
            //baseMapper.insert(BeanUtils.VO2Entity(vo)); //保存角色信息
            saveRoleInfo(vo, roleId);
        } else {
            vo.setRoleId(UUIDUtils.get());
            baseMapper.insert(BeanUtils.VO2Entity(vo)); //保存角色信息
            saveRoleInfo(vo, vo.getRoleId());
        }

    }

    private void saveRoleInfo(SysRoleVO vo, String roleId) {
        String permission = vo.getSysPermissionId();
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(permission)) {
            List<String> permissions = Arrays.asList(permission.split(","));
            permissions.forEach(it -> {
                baseMapper.savePermission(roleId, it);
            });
        }
        String menu = vo.getMenuId();
        List<String> menuList = Arrays.asList(menu.split(","));
        menuList.forEach(it -> {
            baseMapper.saveRoleMenu(roleId, it);
        });
    }

    @Override
    public String queryRoleName(String ids) {
        String name = null;
        name = baseMapper.queryUserByRoleId(ids);
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(name)) {
            return name;
        } else {
            baseMapper.deleteRole(ids);
        }
        return name;
    }

    @Override
    public void deleteRole(String ids) {
        baseMapper.deleteRoleById(ids);
    }

    @Override
    public List<String> getPermission(String userId) {
        return baseMapper.getPermission(userId);
    }
}
